//
//  ShareViewController.m
//  Cat
//
//  Created by lushitong on 2019/1/1.
//  Copyright © 2019年 lushitong. All rights reserved.
//

#import "ShareViewController.h"
#import "ShareBottomView.h"
#import "ShareManager.h"
#import "ProductView.h"
#import "FMDBUser.h"
#import "UserInfoManager.h"
#import "FLTool.h"

@interface ShareViewController ()

@property (nonatomic, strong) ShareBottomView *bottomView;
@property (nonatomic, strong) UILabel *topInfoLabel;
@property (nonatomic, strong) UILabel *secLabel;
@property (nonatomic, strong) UILabel *tileLabel;
@property (nonatomic, strong) ProductView *productDetailView;

@end

@implementation ShareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	[self.topTitleView setDetailDic:@{Nav_Title:@"分享"}.mutableCopy];
	self.view.backgroundColor = [UIColor whiteColor];
	[self loadAllSubView];
	[self setData];
}

- (void)loadAllSubView {
	[self.view addSubview:self.bottomView];
	[self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.and.right.and.bottom.equalTo(self.view);
		make.height.equalTo(@(kQKFitModuleDecimal(58)));
	}];
	
	[self.view addSubviews:@[self.topInfoLabel,self.secLabel]];
	[self.topInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.view.mas_left).offset(kQKFitModuleDecimal(16));
		make.top.equalTo(self.topTitleView.mas_bottom).offset(kQKFitModuleDecimal(12.0));
	}];
	
	[self.secLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.topInfoLabel.mas_left);
		make.right.equalTo(self.view.mas_right).offset(-kQKFitModuleDecimal(16));
		make.top.equalTo(self.topInfoLabel.mas_bottom).offset(kQKFitModuleDecimal(8.0));
	}];
	
	[self.view addSubview:self.productDetailView];
	[self.productDetailView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.view.mas_left).offset(kQKFitModuleDecimal(16));
		make.right.equalTo(self.view.mas_right).offset(-kQKFitModuleDecimal(16));
		make.top.equalTo(self.secLabel.mas_bottom).offset(kQKFitModuleDecimal(8.0));
		make.bottom.equalTo(self.bottomView.mas_top);
	}];
}

- (void)setData {
	self.topInfoLabel.text = [NSString stringWithFormat:@"奖励佣金预估￥%@",self.model.rebate];
	[self.productDetailView setDeatilCoupon:self.model andProduct:self.productModel];
}

#pragma mark----- lazy init  -----

- (ShareBottomView *)bottomView {
	if (!_bottomView) {
		_bottomView = [[ShareBottomView alloc] init];
		FMDBUser *user = [UserInfoManager userWithPhone:UDPhone];
		NSString *shareURL = [NSString stringWithFormat:@"%@/html/invite.html?phone=%@&invite=%@&avatar=%@",HOST,UDPhone,user.inviteCode,user.avator];
		shareURL = [[NSString alloc] initWithData:[shareURL dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES] encoding:NSASCIIStringEncoding];
		_bottomView.backgroundColor = COLOR_WITH_HEX(0xFFFFFF);
		[_bottomView clickShareQQButton:^{
			// 点击分享到QQ
			[ShareManager shareTextWithString:[self getShareText] platformType:UMSocialPlatformType_QQ andWith:self];
		} andWechatButton:^{
			/// 点击分享到微信
			[ShareManager shareTextWithString:[self getShareText] platformType:UMSocialPlatformType_WechatSession andWith:self];
		}];
	}
	return _bottomView;
}

- (UILabel *)topInfoLabel {
	if (!_topInfoLabel) {
		_topInfoLabel = [UILabel labelWithFont:[UIFont boldSystemFontOfSize:kQKFitFont(16)] textColor:COLOR_WITH_HEX(0xFF3F3F)];
	}
	return _topInfoLabel;
}

- (UILabel *)secLabel {
	if (!_secLabel) {
		_secLabel = [UILabel labelWithText:@"复制淘宝口令到推荐给朋友购买,就有佣金拿(分享到朋友圈可以让更多好友看到)" font:[UIFont systemFontOfSize:kQKFitFont(12)] textColor:COLOR_WITH_HEX(0x4C4947)];
		_secLabel.numberOfLines = 0;
	}
	return _secLabel;
}

- (ProductView *)productDetailView {
	if (!_productDetailView) {
		_productDetailView = [[ProductView alloc] init];
		_productDetailView.backgroundColor = COLOR_WITH_HEX(0xFAFAFA);
		_productDetailView.layer.cornerRadius = kQKFitModuleDecimal(2.0);
		_productDetailView.layer.masksToBounds = YES;
	}
	return _productDetailView;
}

- (NSString *)getShareText {
	NSString *text = [[NSString alloc] init];
	text = [NSString stringWithFormat:@"%@\n【在售价】 %@元\n【券后价】 %@元\n【下载喵象再省】 %@元\n复制这条信息￥%@ ,打开【手机淘宝】即可查看",self.productModel.title,self.productModel.marketAmount,self.productModel.buyPrice,self.productModel.prizeAmount,self.model.keyword];
	return text;
}

@end
